// P4KDlg.cpp : implementation file
//

#include "stdafx.h"
#include "YL_DEMO_VOIP.h"
#include "P4KDlg.h"
#include "YL_DEMO_VOIPDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CP4KDlg dialog


CP4KDlg::CP4KDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CP4KDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CP4KDlg)
	m_strLCDText = _T("");
	m_strDTMFKeyText = _T("");
	//}}AFX_DATA_INIT
	m_pParent = pParent;
	cDefaultType = CHANNEL_USB;
}


void CP4KDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CP4KDlg)
	DDX_Text(pDX, IDC_LCDTEXT, m_strLCDText);
	DDX_Text(pDX, IDC_DTMFSTRING, m_strDTMFKeyText);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CP4KDlg, CDialog)
	//{{AFX_MSG_MAP(CP4KDlg)
	ON_BN_CLICKED(IDC_SETRING, OnSetring)
	ON_BN_CLICKED(IDC_SETTONE, OnSettone)
	ON_BN_CLICKED(IDC_SETLCD, OnSetlcd)
	ON_BN_CLICKED(IDC_SETHANDFREE, OnSethandfree)
	ON_BN_CLICKED(IDC_SETLED, OnSetled)
	ON_WM_SHOWWINDOW()
	ON_BN_CLICKED(IDC_SETHEADSET, OnSetheadset)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_CHANNEL, &CP4KDlg::OnBnClickedChannel)
	ON_BN_CLICKED(IDC_PSTNHOLD, &CP4KDlg::OnBnClickedPstnhold)
	ON_BN_CLICKED(IDC_SETDTMFKEY, &CP4KDlg::OnBnClickedSetdtmfkey)
	ON_BN_CLICKED(IDC_DEF_CHANNEL, &CP4KDlg::OnBnClickedDefChannel)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CP4KDlg message handlers

void CP4KDlg::OnSetring() 
{
	static BOOL bRing = TRUE;
	static int iIndex = 0;
	
	YL_DeviceIoControl(YL_IOCTL_SET_RING, (LPVOID)bRing, (LPVOID)iIndex);
	
	if ( bRing )
	{
		CString strText = _T("");
		strText.Format( _T("START_RING %d"), iIndex );
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( strText );
		iIndex = ++iIndex%10;
	}
	else
		GetDlgItem(IDC_RINGSTATE)->SetWindowText( _T("STOP_RING") );
	
	bRing = ! bRing;
}

void CP4KDlg::OnSettone() 
{
	static int eType = PHONE_TONE_SILENCE;
	
	YL_DeviceIoControl(YL_IOCTL_SET_TONE, (LPVOID)eType);
	
	switch ( eType )
	{
	case PHONE_TONE_UNKNOW:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_UNKNOW") );
		break;
	case PHONE_TONE_SILENCE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_SILENCE") );
		break;
	case PHONE_TONE_MUTE:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_MUTE") );
		break;
	case PHONE_TONE_DIAL:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_DIAL") );
		break;
	case PHONE_TONE_CONNECTING:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_CONNECTING") );
		break;
	case PHONE_TONE_RINGBACK:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_RINGBACK") );
		break;
	case PHONE_TONE_BUSY:
		GetDlgItem(IDC_TONESTATE)->SetWindowText( _T("TONE_BUSY") );
		break;
	default:
		break;
	}
	
	eType++;
	
	if ( eType > PHONE_TONE_BUSY )
		eType = PHONE_TONE_SILENCE;
}

void CP4KDlg::OnSetlcd() 
{
	UpdateData( TRUE );

	YL_DeviceIoControl(YL_IOCTL_SET_P4KLCDTEXT, (LPVOID)(LPCTSTR)m_strLCDText);
}

void CP4KDlg::OnSethandfree() 
{
	static BOOL bOpen = TRUE;
	
	YL_DeviceIoControl(YL_IOCTL_OPEN_P4KSPEAK, (LPVOID)bOpen);
	
	if ( bOpen )
		GetDlgItem(IDC_SPEAKSTATE)->SetWindowText( _T("SPEAK_OPEN") );
	else
		GetDlgItem(IDC_SPEAKSTATE)->SetWindowText( _T("SPEAK_CLOSE") );
	
	bOpen = ! bOpen;
}

void CP4KDlg::OnSetled() 
{
	static BOOL bLight = TRUE;

	YL_DeviceIoControl(YL_IOCTL_SET_P4KLED, (LPVOID)bLight);

	if ( bLight )
		GetDlgItem(IDC_LEDSTATE)->SetWindowText( _T("LED_LIGHT") );
	else
		GetDlgItem(IDC_LEDSTATE)->SetWindowText( _T("LED_SHUT") );

	bLight = ! bLight;
}

void CP4KDlg::OnSetheadset() 
{
	static BOOL bOn = TRUE;

	YL_DeviceIoControl(YL_IOCTL_SET_P9KHEADSET, (LPVOID)bOn);
	
	if ( bOn )
		GetDlgItem(IDC_HEADSETSTATE)->SetWindowText( _T("Headset_On") );
	else
		GetDlgItem(IDC_HEADSETSTATE)->SetWindowText( _T("Headset_Off") );

	bOn = !bOn;
}


void CP4KDlg::OnShowWindow(BOOL bShow, UINT nStatus) 
{
	CDialog::OnShowWindow(bShow, nStatus);
	
	DWORD dwVer = 0;
	YL_DeviceIoControl(YL_IOCTL_GET_PHONEVERSION, &dwVer);
	
	BOOL bP9K = IS_P9K(dwVer);
	GetDlgItem(IDC_SETHEADSET)->EnableWindow( bP9K );
	
	GetDlgItem(IDC_CHANNEL)->EnableWindow( IS_DUALPhones(dwVer) );
	GetDlgItem(IDC_PSTNHOLD)->EnableWindow( IS_DUALPhones(dwVer) );
	GetDlgItem(IDC_SETDTMFKEY)->EnableWindow( IS_DUALPhones(dwVer) );
	
	
}


void CP4KDlg::OnBnClickedChannel()
{
	static BOOL bUsbOn = TRUE;

	YL_DeviceIoControl(YL_IOCTL_SET_DUALPHONE_CHANNEL, (LPVOID)bUsbOn);

	if ( bUsbOn )
		GetDlgItem(IDC_STATIC_CHANNEL)->SetWindowText( _T("USB") );
	else
		GetDlgItem(IDC_STATIC_CHANNEL)->SetWindowText( _T("PSTN") );

	bUsbOn = !bUsbOn;
}

void CP4KDlg::OnBnClickedPstnhold()
{
	
	static BOOL bConnected = TRUE;

	YL_DeviceIoControl(YL_IOCTL_SET_PSTN_HOLD, (LPVOID)bConnected);

	if ( bConnected )
		GetDlgItem(IDC_PSTNHOLDSTA)->SetWindowText( _T("CONNECT") );
	else
		GetDlgItem(IDC_PSTNHOLDSTA)->SetWindowText( _T("DISCONNECT") );

	bConnected = !bConnected;
}

void CP4KDlg::OnBnClickedSetdtmfkey()
{
	UpdateData( TRUE );
	YL_DeviceIoControl(YL_IOCTL_SEND_DTMF_KEY, (LPVOID)(LPCTSTR)m_strDTMFKeyText);
}

void CP4KDlg::OnBnClickedDefChannel()
{
	if ( cDefaultType == CHANNEL_PSTN)
	{
		GetDlgItem(IDC_STATIC_DEFCHANNEL)->SetWindowText( _T("DEFAULT USB CHANNEL") );
		cDefaultType = CHANNEL_USB;
	}
	else if ( cDefaultType == CHANNEL_USB)
	{
		GetDlgItem(IDC_STATIC_DEFCHANNEL)->SetWindowText( _T("DEFAULT PSTN CHANNEL") );
		cDefaultType = CHANNEL_PSTN;
	}
	YL_DeviceIoControl(YL_IOCTL_SEND_DEFAULT_CHANNEL, (LPVOID)cDefaultType	);
}
